package cn.soft863.servlet.back;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.soft863.common.util.DateUtil;
import cn.soft863.common.util.PageInfoUtil;
import cn.soft863.users.pojo.AMenu;
import cn.soft863.users.service.MenuService;
import cn.soft863.users.service.impl.MenuServiceImpl;


/**
 * 管理员后台的Servlet
 * 
 * @author 902-B-Book
 *
 */
public class MenuBackServlet extends HttpServlet
{
	private MenuService menuService = new MenuServiceImpl();
	private DateUtil dateUtil = new DateUtil() ; 
	
	/**
	 * 这个是核心的代码,处理请求的代码
	 */
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		/* 只能处理POST请求的乱码 */
		request.setCharacterEncoding("UTF-8");
		
		/* 添加一个操作符 */
		String method = request.getParameter("method");
		
		System.out.println("---UsersbackServlet----service----------" + method);
		if("menuList".equalsIgnoreCase(method))
		{
			/* 管理员列表页面 */
			menuList(request, response);
		}else if("menuInsert".equalsIgnoreCase(method))
		{
			/* 打开管理员的添加页面呢 */
			menuInsert(request, response);
		}else if("menuInsertSubmit".equalsIgnoreCase(method))
		{
			/* 打开管理员的添加提交页面呢 */
			menuInsertSubmit(request, response);
		}else if("menuUpdate".equalsIgnoreCase(method))
		{
			/* 打开管理员的添加页面呢 */
			menuUpdate(request, response);
		}else if("menuUpdateSubmit".equalsIgnoreCase(method))
		{
			/* 打开管理员的添加提交页面呢 */
			menuUpdateSubmit(request, response);
		}
	}

	/**
	 * 管理员更新提交
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void menuUpdateSubmit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		System.out.println("=======menuUpdateSubmit====");
		/* 接收参数
		 * 不管是post还是get,提交的字符串都是以此种格式显示
		 * 键=值&键=值&键=值&键=值&键=值&键=值&键=值&
		 * 参数是键
		 * method=menuUpdate&aid=4
		 * 
		 * 获取Id */
		String id = request.getParameter("aid");
		
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("id", id);
		AMenu menu = this.menuService.findOnemenuService(condMap);
		
		/* 接收参数 */
		String createId = request.getParameter("createId");
		String updateId = request.getParameter("updateId");
		String parentId = request.getParameter("parentId");
		String name = request.getParameter("name");
		String url = request.getParameter("url");
		String content = request.getParameter("content");
		String isLeafStatus = request.getParameter("isLeafStatus");
		String status = request.getParameter("status");
		
		/* 处理参数 */
		int createIdInt =	Integer.parseInt(createId) ;
		int updateIdInt = Integer.parseInt(updateId) ;
		int parentIdInt = Integer.parseInt(parentId) ;	
		byte isLeafStatusByte = Byte.valueOf(isLeafStatus);
		byte statusByte = Byte.valueOf(status);
		

		/* 赋值 */	
		/* 页面上不需要赋值的,后台直接填写 */

		menu.setCreateId(createIdInt);
		menu.setUpdateId(updateIdInt);
		menu.setParentId(parentIdInt);
		menu.setName(name);
		menu.setUrl(url);
		menu.setContent(content);
		menu.setIsLeafStatus(isLeafStatusByte);
		menu.setStatus(statusByte);
		menu.setUpdateTime(new Date());
		
		/* 调用Service的方法,存储结果
		 * Test类,真的就可以将数据存储到数据库了
		 *  */
		int res = this.menuService.updateOnemenuService(menu);
		System.out.println(res + "------------------");
		/* 存储结果 */
		String info = "" ; 
		if(res > 0 )
		{
			/* Sucess */
			info = "成功,id:" + menu.getId() ; 
		}else
		{
			info = "失败" ;
		}
		request.setAttribute("info", info);
		
		
		String url2 = "/WEB-INF/jsp/back/menuUpdate.jsp" ; 
		request.getRequestDispatcher(url2).forward(request, response);
	}
	
	/**
	 * 管理员更新打开页面
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void menuUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		System.out.println("=======menuUpdate====");
		
		/* 接收参数
		 * 不管是post还是get,提交的字符串都是以此种格式显示
		 * 键=值&键=值&键=值&键=值&键=值&键=值&键=值&
		 * 参数是键
		 * method=menuUpdate&aid=4
		 * 
		 * 获取Id */
		String id = request.getParameter("aid");
		
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("id", id);
		AMenu menu = this.menuService.findOnemenuService(condMap);
		/* 将结果存储到四大作用域中 */
		request.setAttribute("menu", menu);
		
		String url = "/WEB-INF/jsp/back/menuUpdate.jsp" ; 
		request.getRequestDispatcher(url).forward(request, response);
	}

	/**
	 * 打开管理员的添加提交页面呢
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void menuInsertSubmit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		System.out.println("======menuInsertSubmit=======");
		
		/* 如何接收页面的信息呢? */
		/* 接收参数 */
		String createId = request.getParameter("createId");
		String updateId = request.getParameter("updateId");
		String parentId = request.getParameter("parentId");
		String name = request.getParameter("name");
		String url = request.getParameter("url");
		String content = request.getParameter("content");
		String isLeafStatus = request.getParameter("isLeafStatus");
		String status = request.getParameter("status");
		
		/* 处理参数 */
		int createIdInt =	Integer.parseInt(createId) ;
		int updateIdInt = Integer.parseInt(updateId) ;
		int parentIdInt = Integer.parseInt(parentId) ;	
		byte isLeafStatusByte = Byte.valueOf(isLeafStatus);
		byte statusByte = Byte.valueOf(status);
		
		/* 将Test类里面的代码拷贝到此处
		 * 
		 * 为menu赋值的时候一切的一切填写的全部是写死的值????
		 * 如何将页面上填写的信息赋值给menu对象呢? 
		 *  */
		/* new对象 */
		AMenu menu = new AMenu() ; 
		/* 赋值 */	
		/* 页面上不需要赋值的,后台直接填写 */

		menu.setCreateId(createIdInt);
		menu.setUpdateId(updateIdInt);
		menu.setParentId(parentIdInt);
		menu.setName(name);
		menu.setUrl(url);
		menu.setContent(content);
		menu.setIsLeafStatus(isLeafStatusByte);
		menu.setStatus(statusByte);
		menu.setCreateTime(new Date());
		menu.setUpdateTime(new Date());
		menu.setPubTime(new Date());

		
		/* 调用Service的方法,存储结果
		 * Test类,真的就可以将数据存储到数据库了
		 *  */
		int res = this.menuService.saveOnemenuService(menu);
		System.out.println(res + "------------------");
		/* 存储结果 */
		String info = "" ; 
		if(res > 0 )
		{
			/* Sucess */
			info = "成功,id:" + menu.getId() ; 
		}else
		{
			info = "失败" ;
		}
		request.setAttribute("info", info);
		/* 服务器端跳转到管理员添加的jsp上面 */
		String url2 = "/WEB-INF/jsp/back/menuInsert.jsp" ; 
		request.getRequestDispatcher(url2).forward(request, response);
	}
	
	/**
	 * 打开管理员的添加页面呢
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void menuInsert(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		System.out.println("======menuInsert=======");
		/* 服务器端跳转到管理员添加的jsp上面 */
		String url = "/WEB-INF/jsp/back/menuInsert.jsp" ; 
		request.getRequestDispatcher(url).forward(request, response);
	}

	/**
	 * 管理员列表页面
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void menuList(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		/* 要想翻页,得让当前页发生变化才行 
		 * 需要从页面上获取当前页和每页多少条
		 * 
		 * 不管是post还是get,是表单还是url
		 * 所传递的参数对都是键=值&键=传&键=传&键=传&键=传&键=传
		 * 
		 * String 值 = request.getParameter("键");
		 * */
		String currentPage = request.getParameter("currentPage");
		String pageSize = request.getParameter("pageSize");
		
		PageInfoUtil pageInfoUtil = new PageInfoUtil() ; 
		/* currentPage如何木有传或者传入的不是整数,咋办?
		 * 转换数据会报错,咋办? */
		try
		{
			pageInfoUtil.setCurrentPage(Integer.valueOf(currentPage));
			pageInfoUtil.setPageSize(Integer.valueOf(pageSize));
		} catch (NumberFormatException e)
		{
		}
		
		
		/* 传搜索要用的
		 * 
		 * 凡是Servlet需要的东西,需要的参数,都问jsp要,都从request里面取
		 *  */
		String keyword = request.getParameter("keyword");
		String status = request.getParameter("status");
		String st = request.getParameter("st");
		String ed = request.getParameter("ed");
		
		if(keyword == null)
		{
			keyword = ""; 
		}
		/* 取掉前后空格 */
		keyword = keyword.trim() ;
		if(status == null)
		{
			status = "" ; 
		}
		if(st == null)
		{
			st = "" ; 
		}
		if(ed == null)
		{
			ed = "" ; 
		}
		
		Date stDate = null ; 
		Date edDate = null ; 
		
		/* 如何将字符串的日期时间变成Date对象呢? */
		if(!"".equalsIgnoreCase(st) && !"".equalsIgnoreCase(ed))
		{
			stDate = dateUtil.strToDateTime(st);
			edDate = dateUtil.strToDateTime(ed);
		}
		
		Map<String, Object> condMap = new HashMap<String, Object>();
		/* 按照关键字查询 */
		condMap.put("keyword", keyword);
		/* 按照状态查询 */
		condMap.put("status", status);
		condMap.put("stDate", stDate);
		condMap.put("edDate", edDate);
		
		/**
		 * 终于将查询的结果放到了集合中
		 */
		List<AMenu> menuList = this.menuService.findCondListmenuService(pageInfoUtil,condMap);
		/* 将Service调用方法的返回值放到四大作用域中. */
		request.setAttribute("menuList", menuList);
		request.setAttribute("pageInfoUtil", pageInfoUtil);
		
		/* 存储搜索条件 */
		request.setAttribute("keyword", keyword);
		request.setAttribute("status", status);
		request.setAttribute("st", st);
		request.setAttribute("ed", ed);
		
		String url = "/WEB-INF/jsp/back/menuList.jsp" ; 
		/* 服务器端跳转到jsp上面.
		 * 所有的jsp都放到web-inf/jsp/back中 */
		request.getRequestDispatcher(url).forward(request, response);
	}
}
